//package com.example.demo.bean;
//
//
///**
// * @program: SpringAOP
// * @ClassName GeoTiffExample
// * @description:
// * @author: zsn
// * @create: 2024-07-30 16:00
// * @Version 1.0
// **/
//
//
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.geotools.coverage.grid.GridCoverage2D;
//import org.geotools.coverage.grid.GridCoverageFactory;
//import org.geotools.coverage.grid.io.AbstractGridFormat;
//import org.geotools.gce.geotiff.GeoTiffFormat;
//import org.geotools.gce.geotiff.GeoTiffWriteParams;
//import org.geotools.geometry.jts.ReferencedEnvelope;
//import org.geotools.referencing.crs.DefaultGeographicCRS;
//import org.opengis.coverage.grid.GridCoverageWriter;
//import org.opengis.parameter.GeneralParameterValue;
//import org.opengis.parameter.ParameterValueGroup;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import javax.imageio.ImageIO;
//import javax.media.jai.TiledImage;
//import java.awt.image.BufferedImage;
//import java.awt.image.WritableRaster;
//import java.io.File;
//import java.io.IOException;
//
//import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
//
//@Slf4j
//@Service
//@RequiredArgsConstructor
//public class TiffServiceImpl{
//
//    @Value("${system.google-images-path}")
//    private String googleSatellitePath;
//
//    private String image_ext = ".jpg";
//
//
//
//    public void tile2Tiff() throws IOException {
//        /*// 获取x、y的数字最大值和最小值
//        int[] minXY = WGSUtil.wgs_to_tile(left, top);
//        int[] maxXY = WGSUtil.wgs_to_tile(right, bottom);
//        int posi1x = minXY[0];
//        int posi1y = minXY[1];
//        int posi2x = maxXY[0];
//        int posi2y = maxXY[1];
//
//        int lenX = posi2x - posi1x + 1;
//        int lenY = posi2y - posi1y + 1;
//        BufferedImage image = null;
//        WritableRaster raster = null;
//        BufferedImage defaultImage = null;
//        try {
//            defaultImage = ImageIO.read(this.getDefaultSatelliteFile());
//        } catch (IOException e) {
//            log.error("读取默认卫星影像栅格图片失败，请检查文件是否存在");
//            return;
//        }*/
//        WritableRaster raster = null;
//        File file=new File("C:\\Users\\zsn\\Desktop\\image\\image-灰度\\202407191626_202407191600.tif");
//        //BufferedImage image = ImageIO.read(input);
//        BufferedImage blank = ImageIO.read(file);
//        raster = blank.copyData(null);
//
//        /*// 循环获取图片并生成WritableRaster对象
//        for (int i = posi1x; i <= posi2x; i++) {
//            for (int j = posi1y; j <= posi2y; j++) {
//                try {
//                    image = ImageIO.read(this.getSatelliteFile(i, j));
//                } catch (IOException e) {
//                    image = defaultImage;
//                }
//                raster.setRect((i - posi1x) * 256, (j - posi1y) * 256, image.getRaster());
//            }
//        }*/
//
//
//
//        // 设置包围盒
//        ReferencedEnvelope envelope = new ReferencedEnvelope(107.85, 119.1, 28.7, 38.7, DefaultGeographicCRS.WGS84);
//
//        // 创建网格
//        GridCoverageFactory factory = new GridCoverageFactory();
//        GridCoverage2D gridCoverage = factory.create("my_grid", raster, envelope);
//
//        // 创建tiff生成参数
//        GeoTiffFormat format = new GeoTiffFormat();
//        GeoTiffWriteParams wp = new GeoTiffWriteParams();
//        wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
//        wp.setCompressionType("Deflate");
//        ParameterValueGroup params = format.getWriteParameters();
//        params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp);
//        GeneralParameterValue[] writeParameters = params.values().toArray(new GeneralParameterValue[1]);
//
//        // gridCoverage
//        GeoTiffFormat format1 = new GeoTiffFormat();
//        File out = new File("C:\\Users\\zsn\\Desktop\\image\\坐标\\1.tif");
//        GridCoverageWriter writer = format1.getWriter(out);
//        try {
//            writer.write(gridCoverage, writeParameters);
//            writer.dispose();
//        } catch (IOException e) {
//            log.error("生成tiff失败：{}", e.getMessage());
//        }
//
//        gridCoverage.dispose(true);
//    }
//
//    /**
//     * 获取卫星影像图片
//     *
//     * @param x
//     * @param y
//     * @return
//     */
//    private File getSatelliteFile(int x, int y) {
//        return new File(this.googleSatellitePath + 18 + File.separator + x + File.separator + y + this.image_ext);
//    }
//
//    /**
//     * 获取默认的卫星影像图片
//     *
//     * @return
//     */
//    private File getDefaultSatelliteFile() {
//        return new File("C:\\Users\\zsn\\Desktop\\image\\image-灰度\\202407191626_202407191600.tif");
//    }
//
//    public static void main(String[] args) throws IOException {
////        com.example.demo.config.TiffServiceImpl tiffService=new com.example.demo.config.TiffServiceImpl();
////        tiffService.tile2Tiff();
//    }
//}